package net.diaowen.dwsurvey.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.diaowen.common.base.dao.WcUserDao;
import net.diaowen.common.base.dao.WcUserDetailDao;
import net.diaowen.common.base.entity.User;
import net.diaowen.common.base.entity.WcUser;
import net.diaowen.common.base.entity.WcUserDetail;
import net.diaowen.common.service.BaseServiceImpl;
import net.diaowen.dwsurvey.service.UserManager;
import net.diaowen.dwsurvey.service.WechatManager;

@Service
public class WechatManagerImpl extends BaseServiceImpl<WcUser, String> implements WechatManager {
	
	@Autowired
	private WcUserDao wcUserDao;
	
	@Autowired
	private WcUserDetailDao wcUserDetailDao;

	@Autowired
	private UserManager userManager;
	
	
	@Override
	public void setBaseDao() {
		this.baseDao=wcUserDao;
		
	}

	@Override
	public int releaseBind(String id) {
		wcUserDao.delete(id);
		return 0;
	}

	@Override
	public WcUser findUserByWxid(String wxid) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("wxId", wxid));
		return wcUserDao.findFirst(criterions);
	}

	@Override
	public WcUser findUserByUserid(String userId) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("userId", userId));
		return wcUserDao.findFirst(criterions);
	}

	@Override
	public int releaseBindByUserId(String userId) {
		WcUser wcUser = findUserByUserid(userId);
		if (wcUser == null) {
			return 0;
		}
		wcUserDao.delete(wcUser);
		return 0;
	}

	@Override
	public WcUserDetail getWcUserDetail(String wxid) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("wxId", wxid));
		return wcUserDetailDao.findFirst(criterions);
	}

	@Override
	public void saveWcUserDetail(WcUserDetail wcd) {
		wcUserDetailDao.save(wcd);
		
	}
}
